package org.vectrics.recruiter.struts;

import org.apache.log4j.Category;
import org.vectrics.recruiter.user.RecruitUserProfile;
import org.vectrics.recruiter.job.*;
import java.util.*;
import org.apache.struts.action.Action;

public class SaveDetailSkillsAction extends Action {
    private static Category log = Category.getInstance(SaveDetailSkillsAction.class);
    public static String ACTION_NAME = "save_detail_skills";
    private RecruitUserProfile userProfile = null;
    private Collection skillSet = null;
    private boolean usingWizard = true;
    private int currentLevel = 0;
    private boolean needMoreDetails = false;
    private boolean save = false;
    private Job job = null;
    
    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param request The HTTP request we are processing
     *
     */
    /*
    protected void doPerform(Request request) {
 
        log.debug("perform() called.");
        String jobId = request.getParameter("job");
        SkillRatingOwner owner = null;
        
        
        String levelParam = request.getParameter("current_level");
        
        // Forced save request 

        String saveParam = request.getParameter("save");
        if ((saveParam != null) && (saveParam.startsWith("true"))) {
            save = true;
        }
        
        if (levelParam != null)
            currentLevel = Integer.parseInt(levelParam);
        else 
            currentLevel = -1;
        
        log.debug("doPerform() - current level = " + currentLevel);
        needMoreDetails = false;
        
        if (jobId != null) {
            log.debug("doPerform() - job param passed, finding job");
            job = RecruitManager.getInstance().findJob(jobId);
            owner = job;
            if (job != null) {
                log.debug("doPerform() - job found for param");
                skillSet = job.findAllSkillRatings();
            }
            else {
                log.error("doPerform() - job not found");
            }
        }
        else {
            log.debug("doPerform() - finding user profile for skill entry");
            userProfile = RecruitManager.getUserProfile(request);
            owner = userProfile.getRecruitUserComponent();
            try {
                log.debug("doPerform() - finding skill set for user profile");
                skillSet = userProfile.getUserSkillSet();
            }
            catch (Exception ex) {
                log.error("Exception finding user skill set", ex);
            }
        }

        if ((save) && (job != null))
            usingWizard = true;
        
    
        try {
            // Remove all the skills for the level just submitted,  this procedure
            // will add the ones the user selected
            if ((usingWizard) && (currentLevel >= 0)) {
                SkillSetUtil util = new SkillSetUtil(owner, RecruitManager.getInstance().getSkillTree());                
                util.removeSkillsForLevel(currentLevel + 1);
            }
            
            Enumeration paramEnum = request.getHttpRequest().getParameterNames();
            log.debug("Master skillset length = " + RecruitManager.getInstance()
                        .getSkillTree().getMasterList().size());
            while (paramEnum.hasMoreElements()) {
                String paramName = (String)paramEnum.nextElement();
                String value = request.getParameter(paramName);
                log.debug("doPerform() - looking at param -  name : " + paramName);                        
                log.debug("doPerform() - looking at param - value : " + value);                        

                
                if (paramName.startsWith("yrs_")) {
                    log.debug("doPerform() - yrs_ param found.  Setting skill years.");
                    if  (value.trim().length() > 0) {
                                
                        int valueInt = 0;
                        try {
                            valueInt = Integer.parseInt(value);
                        }
                        catch (Exception ex) {
                            log.error(ex);
                            valueInt = -1;
                        }
                                
                        if (valueInt > 0) {
                            log.debug("doPerform() - yrs rating found: " + paramName);
                            String skillCode = paramName.substring(4);
                            Skill skill = (Skill)RecruitManager.getInstance().getSkillTree().find(skillCode);
                            if (skill != null) {
                                if (needMoreDetails == false) 
                                    needMoreDetails = (skill.getChildren().size() > 0);
                                SkillRating rating = owner.findSkillRating(skill);
                                if (rating == null) {
                                    log.debug("doPerform() - creating new skill rating.");
                                    rating = owner.createSkillRating(skill);
                                    rating.setWeight(0);
                                    
                                    if (skill.getChildren().size() > 0) {
                                        needMoreDetails = true;
                                    }
                                }
                                log.debug("Setting yrs to " + value);
                                rating.setYears(valueInt);
                            }
                            else {
                                log.error("doPerform() - Could not find skill for param: "
                                        + paramName + "  Code = " + skillCode);
                            };
                        }
                    }
                    else {
                        log.debug("doPerform() - yrs_ param had blank value - ignored");
                    }
                }
                else if (paramName.startsWith("lvl_")) {
                    if (value.trim().length() > 0) { 
                        log.debug("doPerfom() - 'lvl-*** - rating level found.");
                        String skillCode = paramName.substring(4);
                        Skill skill = (Skill)RecruitManager.getInstance().getSkillTree().find(skillCode);
                        if (skill != null) {
                            if (needMoreDetails == false) { 
                                needMoreDetails = (skill.getChildren().size() > 0);
                                if (needMoreDetails) {
                                    log.debug("doPerform() - needMoreDetails set to true while setting rating level.");
                                }
                            }
                            SkillRating rating = owner.findSkillRating(skill);
                            int ratingLevel = Integer.parseInt(value);
                            if ((rating == null) && (ratingLevel > 0)) {
                                log.debug("doPerform() - found lvl - creating new skill rating.");
                                
                                rating = owner.createSkillRating(skill);

                                if (ratingLevel <= 0)
                                    rating.setWeight(0);
                                else 
                                    rating.setWeight(100);
                                rating.setYes(false);
                                rating.setSkill(skill);
                            }
                            
                            if (rating != null) {
                                log.debug("Setting lvl to " + value);
                                rating.setLevel(ratingLevel);
                            }
                            
                        }
                        else {
                            log.error("doPerform() - Could not find skill for param: "
                                    + paramName + "  Code = " + skillCode);
                        };
                    }
                    else {
                        log.debug("doPerform() - lvl_ param had blank value.  Ignored.");
                    }
                }
                else if (paramName.startsWith("yn_")) {
                    String skillCode = paramName.substring(3);
                    log.debug("doPerform() - checked: " + skillCode);
                    Skill skill = (Skill)RecruitManager.getInstance().getSkillTree().find(skillCode);

                    if (skill != null) {
                        if (needMoreDetails == false) { 
                            needMoreDetails = (skill.getChildren().size() > 0);
                        }
                        else {
                            log.debug("Checked skill has no children (y/n checkbox). Skill = " + skillCode);
                        }
                        
                        SkillRating rating = owner.findSkillRating(skill);
                        if (rating == null) {
                            rating = owner.createSkillRating(skill);
                            rating.setWeight(100);
                            rating.setLevel(0);
                        }
                        rating.setYes(true);
                    }
                    else {
                        log.error("doPerform() - Could not find skill for param: "
                                + paramName + "  Code = " + skillCode);
                    };
                }
                else if (paramName.startsWith("children_")) {
                    String skillCode = paramName.substring(9);
                    log.debug("doPerform() - view children checked, skill = " + skillCode);
                    Skill skill = (Skill)RecruitManager.getInstance().getSkillTree().find(skillCode);
                    
                    if (skill != null) {
                        if (needMoreDetails == false) 
                            needMoreDetails = (skill.getChildren().size() > 0);
                        else {
                            log.debug("Checked skill has no children (children checkbox). Skill = " + skillCode);
                        }
                        SkillRating rating = owner.findSkillRating(skill);
                        if (rating == null) {
                            rating = owner.createSkillRating(skill);
                            rating.setWeight(0);
                            rating.setLevel(0);
                            rating.setYes(false);
                            rating.setSkill(skill);
                        }
                        
                        rating.setViewChildren(true);
                    }
                    else {
                        log.error("doPerform() - Could not find skill for param: "
                                + paramName + "  Code = " + skillCode);
                    };
                }
                else {
                    log.debug("doPerform() - param did not match a pattern: " + paramName);
                }
                
            };
            saveInformation(request);
        }
        catch (Exception ex) {
            log.error("Exception in doPerform: " , ex);
            request.addError("Error occurred performing save of skills.");
        }

        //end
         
    }
         */

    
/*    
    void saveInformation(HttpServletRequest request) {    
        log.debug("saveInformation()");

        String prevParam = request.getParameter("prev_btn");
        if (prevParam != null) {
            log.debug("saveInformation() - previous button pressed - setting needMoreInformation to TRUE");
            needMoreDetails = true;
        }
        
//                if (needMoreDetails) {
        String nextParam = request.getParameter("next_btn");
        int level = currentLevel;
        if (prevParam != null) {
            log.debug("doPerform() - previous button NOT pressed");
            level--;
            if (level >= 0) {
                log.debug("doPerform() - previous button pressed  - going back one entry form.  Level = " + level);
                request.addParam("level", "" + level);

                if (job != null) {
                    setNextForm("/recruit/admin/job/skill/enter");
                }
                else {
                    setNextForm("/recruit/main/user/skill/enter");
                }
            }
            else {
                log.debug("doPerform() - previous button pressed on first entry form - going back to summary");
                if (job != null) {
                    setNextForm("/recruit/admin/job/skill");
                }
                else {
                    setNextForm("/recruit/main/user/skill");
                }
            }
        }
        else if (nextParam != null) {
            log.debug("doPerform() - next button pressed");
            level++;
            if (job != null) {
                setNextForm("/recruit/admin/job/skill/enter");
            }
            else { 
                setNextForm("/recruit/main/user/skill/enter");
            }
            request.addParam("level", "" + level);
            log.debug("doPerform() - next button pressed  - advancing one entry form.  Level = " + level);
        }
        else {
            log.warn("doPerform() - prev button nor Next btn pressed...");
        }
        
        // Clear children of parents that were wiped out on the edited level.
        log.debug("doPerform() before clearing branches that were not choosen by user.\r\n: " + skillSet.toString());
        if (level > 0) {
            
            //skillSet.clearBranchesOfUnratedParents(level);
        }
        else {
            log.debug("doPerform() level is '0' - will not clear unrated branches.");
        }
        log.debug("doPerform() after clearing dead branches User Skills\r\n: " + skillSet.toString());

        if (job == null) {
             if ((needMoreDetails == false) && (save == false)) {
                 request.addParameter("edit_skills", "true");                            
                 setNextForm("/recruit/main/user/skill");
             }

            if (save == true) {
                log.debug("doPerform() - saving job skills");
                setNextForm("/recruit/main/user/profile");
            }
         }
         else {
             if ((needMoreDetails == false) && (save == false)) {
                 request.addParameter("edit_skills", "true");                            
                 setNextForm("/recruit/admin/job/skill");
             }
             
             if (save == true) {
                log.debug("doPerform() - saving job skills");
                //job.setSkillSet((JobSkillSet)skillSet);
                setNextForm("/recruit/admin/job");
             }
         }

    }    
*/
}